<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<HTML>
<HEAD>
<LINK REL=STYLESHEET TYPE="text/css" HREF="../../stylesheet.css" TITLE="Style">
<META NAME="GENERATOR" CONTENT="Java2HTML Version 1.5">
<TITLE>czestmyr.jjsched.StringParser (Java2HTML)</TITLE>
</HEAD>
<BODY><TABLE id="Header" border="0" cellpadding="0" cellspacing="0" width="100%">
<tr>
<td colspan="2" width="33%">&nbsp;</td>
<td align="center" colspan="2" width="33%">
<font size="4">StringParser.java</font>
</td>
<td align="right" colspan="2" width="33%">&nbsp;</td>
</tr>
</TABLE>
<pre ID="Classes">
<A NAME="1"></A><FONT ID="MultiLineComment">/*  This file is a part of JJSched source code
<A NAME="2"></A>    JJSched - Java Jabber Scheduler - a simple Jabber scheduling application
<A NAME="3"></A>    Copyright (C) 2008 Cestmir "CZestmyr" Houska
<A NAME="4"></A>
<A NAME="5"></A>    This program is free software: you can redistribute it and/or modify
<A NAME="6"></A>    it under the terms of the GNU General Public License as published by
<A NAME="7"></A>    the Free Software Foundation, either version 3 of the License, or
<A NAME="8"></A>    (at your option) any later version.
<A NAME="9"></A>
<A NAME="10"></A>    This program is distributed in the hope that it will be useful,
<A NAME="11"></A>    but WITHOUT ANY WARRANTY; without even the implied warranty of
<A NAME="12"></A>    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
<A NAME="13"></A>    GNU General Public License for more details.
<A NAME="14"></A>
<A NAME="15"></A>    You should have received a copy of the GNU General Public License
<A NAME="16"></A>    along with this program.  If not, see &lt;http://www.gnu.org/licenses/&gt; */</FONT>
<A NAME="17"></A>
<A NAME="18"></A><FONT ID="Package">package</FONT> czestmyr.jjsched;
<A NAME="19"></A>
<A NAME="20"></A><FONT ID="Import">import</FONT> java.util.*;
<A NAME="21"></A><FONT ID="Import">import</FONT> java.lang.*;
<A NAME="22"></A>
<A NAME="23"></A><FONT ID="Import">import</FONT> org.jivesoftware.smack.util.*;
<A NAME="24"></A>
<A NAME="25"></A><FONT ID="FormalComment">/**
<A NAME="26"></A> * Contains helper static methods that parse or process strings
<A NAME="27"></A> */</FONT>
<A NAME="28"></A><FONT ID="Public">public</FONT> <FONT ID="Class">class</FONT> StringParser{
<A NAME="29"></A>    <FONT ID="FormalComment">/**
<A NAME="30"></A>     * Parse the given line of text into sequence of tokens
<A NAME="31"></A>     * 
<A NAME="32"></A>     * @param   line    the line of text to be parsed
<A NAME="33"></A>     * @return      the Vector containing the parsed tokens as Strings
<A NAME="34"></A>     */</FONT>
<A NAME="35"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> LinkedList&lt;String&gt; parseLine(String line){
<A NAME="36"></A>        <FONT ID="SingleLineComment">//The return value
<A NAME="37"></A></FONT>        LinkedList&lt;String&gt; parsedTokens = <FONT ID="New">new</FONT> LinkedList&lt;String&gt;();
<A NAME="38"></A>        <FONT ID="If">if</FONT> (line == <FONT ID="Null">null</FONT>) <FONT ID="Return">return</FONT> parsedTokens;
<A NAME="39"></A>        <FONT ID="SingleLineComment">//Skip empty lines
<A NAME="40"></A></FONT>        <FONT ID="If">if</FONT> (line.length() == <FONT ID="IntegerLiteral">0</FONT>) <FONT ID="Return">return</FONT> parsedTokens;
<A NAME="41"></A>        <FONT ID="SingleLineComment">//Don't parse comments
<A NAME="42"></A></FONT>        <FONT ID="If">if</FONT> (line.charAt(<FONT ID="IntegerLiteral">0</FONT>) == <FONT ID="CharacerLiteral">'#'</FONT>) <FONT ID="Return">return</FONT> parsedTokens;
<A NAME="43"></A>
<A NAME="44"></A>        <FONT ID="SingleLineComment">//Initially we are not quoted 
<A NAME="45"></A></FONT>        <FONT ID="Boolean">boolean</FONT> quoted = <FONT ID="False">false</FONT>;
<A NAME="46"></A>
<A NAME="47"></A>        <FONT ID="SingleLineComment">//Divide the line into sequences separated by double quotes
<A NAME="48"></A></FONT>        List multiTokens = Arrays.asList(line.split(<FONT ID="StringLiteral">"\""</FONT>, -<FONT ID="IntegerLiteral">1</FONT>));
<A NAME="49"></A>        <FONT ID="SingleLineComment">//Iterate over these sequences and parse unquoted seqences
<A NAME="50"></A></FONT>        <FONT ID="SingleLineComment">//further
<A NAME="51"></A></FONT>        Iterator it = multiTokens.iterator();
<A NAME="52"></A>        <FONT ID="While">while</FONT> (it.hasNext()){
<A NAME="53"></A>            String multiToken = (String)it.next();
<A NAME="54"></A>            <FONT ID="If">if</FONT> (quoted){
<A NAME="55"></A>                <FONT ID="SingleLineComment">//Even empty string gets appended if it is quoted
<A NAME="56"></A></FONT>                parsedTokens.add(multiToken);
<A NAME="57"></A>            } <FONT ID="Else">else</FONT> {
<A NAME="58"></A>                <FONT ID="SingleLineComment">//Strip leading and tailing whitechars
<A NAME="59"></A></FONT>                multiToken = multiToken.replace(<FONT ID="StringLiteral">"^\\s*"</FONT>, <FONT ID="StringLiteral">""</FONT>);
<A NAME="60"></A>                multiToken = multiToken.replace(<FONT ID="StringLiteral">"\\s*$"</FONT>, <FONT ID="StringLiteral">""</FONT>);
<A NAME="61"></A>
<A NAME="62"></A>                <FONT ID="SingleLineComment">//Skip blank sequences of characters
<A NAME="63"></A></FONT>                <FONT ID="If">if</FONT> (multiToken.length() == <FONT ID="IntegerLiteral">0</FONT>) {
<A NAME="64"></A>                    quoted = !quoted;
<A NAME="65"></A>                    <FONT ID="Continue">continue</FONT>;
<A NAME="66"></A>                }
<A NAME="67"></A>
<A NAME="68"></A>                <FONT ID="SingleLineComment">//Tokenize the sequence
<A NAME="69"></A></FONT>                StringTokenizer tokenizer = <FONT ID="New">new</FONT> StringTokenizer(multiToken);
<A NAME="70"></A>                <FONT ID="While">while</FONT> (tokenizer.hasMoreTokens()){
<A NAME="71"></A>                    parsedTokens.add(tokenizer.nextToken());
<A NAME="72"></A>                }
<A NAME="73"></A>            }
<A NAME="74"></A>
<A NAME="75"></A>            quoted = !quoted;
<A NAME="76"></A>        }
<A NAME="77"></A>
<A NAME="78"></A>        <FONT ID="Return">return</FONT> parsedTokens;
<A NAME="79"></A>    }
<A NAME="80"></A>
<A NAME="81"></A>    <FONT ID="FormalComment">/**
<A NAME="82"></A>     * Beautifies the string.
<A NAME="83"></A>     * The beautification is done by removing all tailing and leading whitespaces
<A NAME="84"></A>     * and removal of sequences of whitespaces, longer than one
<A NAME="85"></A>     *
<A NAME="86"></A>     * @param   str The string to be beautified
<A NAME="87"></A>     * @return      Beautified string
<A NAME="88"></A>     */</FONT>
<A NAME="89"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> String beautifyString(String str){
<A NAME="90"></A>        String output = str.replaceFirst(<FONT ID="StringLiteral">"^\\s+"</FONT>,<FONT ID="StringLiteral">""</FONT>);
<A NAME="91"></A>        output = output.replaceFirst(<FONT ID="StringLiteral">"\\s+$"</FONT>,<FONT ID="StringLiteral">""</FONT>);
<A NAME="92"></A>        output = output.replaceAll(<FONT ID="StringLiteral">"\\s+"</FONT>,<FONT ID="StringLiteral">" "</FONT>);
<A NAME="93"></A>        <FONT ID="Return">return</FONT> output;
<A NAME="94"></A>    }
<A NAME="95"></A>
<A NAME="96"></A>    <FONT ID="FormalComment">/**
<A NAME="97"></A>     * Escapes the jid and selects only username and server from it.
<A NAME="98"></A>     *
<A NAME="99"></A>     * @param   jid The string that contains the Jabber ID
<A NAME="100"></A>     * @return      Escaped JID that consists of username and server name only
<A NAME="101"></A>     */</FONT>
<A NAME="102"></A>    <FONT ID="Public">public</FONT> <FONT ID="Static">static</FONT> String escapeJID(String jid){
<A NAME="103"></A>        <FONT ID="Return">return</FONT> StringUtils.escapeNode(StringUtils.parseName(jid)) + <FONT ID="StringLiteral">"@"</FONT> +
<A NAME="104"></A>               StringUtils.escapeNode(StringUtils.parseServer(jid));
<A NAME="105"></A>    }
<A NAME="106"></A>}
<A NAME="107"></A></pre>
</BODY>
</HTML>